import { reactive, } from 'vue'
import { PORT1 } from "@/api/config/servicePort";
import http from "@/api";
import { contractOptions } from "@/stores/modules/contract.ts";
import { stringify } from "qs";

export let authList = reactive({});

export const enum Path {

    //已签合同
    signedField = "/personnel/user-contracts-signed/field",
    signedList = "/personnel/user-contracts-signed/list",

    //未签合同
    unSignedField = "/personnel/user-contracts-unsigned/field",
    unSignedList = "/personnel/user-contracts-unsigned/list",

    //合同记录
    recordSignedField = "/personnel/user-contracts/field",
    recordSignedList = "/personnel/user-contracts/list?get_auth=1&&auth_name=signedRecord",

    //合同概况
    profileSignedField = "/personnel/contract-manage/field",
    profileSignedList = "/personnel/contract-manage/contract-view",

    //合同公司管理
    contractCompanyField = "/personnel/contract-companies/field",
    contractCompanyList = "/personnel/contract-companies/list",
    contractCompanySave = "/personnel/contract-companies/save",
    contractCompanyDetele = "/personnel/contract-companies/delete",

    //getOrgsMenus
    getOrgsMenus = "/personnel/orgs/menus",
    getUsers = "/personnel/contract-manage/get-users",
    getContractManageOptions = "/personnel/contract-manage/get-options",
    saveContractManageOptions = "/personnel/contract-manage/save",

    //合同表单编辑状态下获取表单内容
    getContractInfo = "/personnel/contract-manage/get-contract-by-id",
    getSearchOptions = "/personnel/user-contracts-signed/search-item",
    getUserOptions = "/personnel/contract-manage/get-users"
}


//已签合同
export const signedGetTableColumns = (params: FormData) => {
    return http.post(PORT1 + Path.signedField, params, { cancel: false, loading: false });
};

export const signedGetTableData = (params) => {

    return http.post(PORT1 + Path.signedList, params, { cancel: false, loading: false });
};


//未签合同
export const unSignedGetTableColumns = (params: FormData) => {
    return http.post(PORT1 + Path.unSignedField, params, { cancel: false, loading: false });
};

export const unSignedGetTableData = (params) => {

    return http.post(PORT1 + Path.unSignedList, params, { cancel: false, loading: false });
};


//合同记录
export const recordSignedGetTableColumns = (params: FormData) => {
    //表头
    return http.post(PORT1 + Path.recordSignedField, params, { cancel: false, loading: false });
};

export const recordSignedGetTableData = (params) => {
    //列表
    return http.post(PORT1 + Path.recordSignedList, params, params, { cancel: false, loading: false });
};



//合同概况
export const profileSignedGetTableColumns = (params: FormData) => {
    //表头
    return http.post(PORT1 + Path.profileSignedField, params, { cancel: false, loading: false });
};

export const profileSignedGetTableData = (params) => {
    //列表
    return http.post(PORT1 + Path.profileSignedList, params, { cancel: false, loading: false });
};

//合同公司管理
export const contractCompanyField = (params: FormData) => {
    //表头
    return http.post(PORT1 + Path.contractCompanyField, params, { cancel: false, loading: false });
};

export const contractCompanyList = (params) => {
    //列表
    return http.post(PORT1 + Path.contractCompanyList, params, { cancel: false, loading: false });
};

export const contractCompanySave = (params) => {
    //保存
    return http.post(PORT1 + Path.contractCompanySave, params, { cancel: false, loading: false });
};

export const contractCompanyDetele = (params) => {
    //删除
    return http.post(PORT1 + Path.contractCompanyDetele, params, { cancel: false, loading: false });
};

//获取页面url
export const getRouterParams = (router) => {
    return Promise.resolve(Object.assign({}, router.currentRoute.value.query))
}


//获取 公司到岗位数据
export const getOrgsMenus = (params) => {
    //列表
    return http.post(PORT1 + Path.getOrgsMenus, params, { cancel: false, loading: false });
};

//获取员工
export const getUsers = (params) => {

    return http.post(PORT1 + Path.getUsers, params, { cancel: false, loading: false });
};
//获取其它下拉数据
export const getContractManageOptions = (params) => {

    return http.get(PORT1 + Path.getContractManageOptions, params, { cancel: false, loading: false });
};
//保存数据
export const saveContractManageOptions = (params) => {

    return http.post(PORT1 + Path.saveContractManageOptions, params, { cancel: false, loading: false });
};



//根据合同号获取表单编辑结果value
export const getContractInfo = (params) => {
    return http.get(PORT1 + Path.getContractInfo, params, { cancel: false, loading: false });
};
//根据合同号获取表单编辑结果value
export const getUserOptions = (params) => {
    return http.post(PORT1 + Path.getUserOptions, params, { cancel: false, loading: false });
};


//获取高级搜索表单内容
export const getSearchOptions = (params) => {
    // let stores = contractOptions();
    // let storeData = stores.getData;
    // let storeDataStatus = stores.getStatus;

    // if (stores.getStatus) {//数据存在
    //     return Promise.resolve(stores.getData);
    // }
    console.log(params)
    console.log()
    return http.post(PORT1 + Path.getSearchOptions + get_auth().setParams(params), params, { cancel: false, loading: false }).then(res => {
        if (res.code == ResultEnum.SUCCESS) {

            let data = formartData(res.data);
            // stores.setStatus(true);
            // stores.setData(data);
            console.log(data)
            get_auth().getAuthList(res, params)
            return Promise.resolve(data)
            // return Promise.resolve(stores.getData)
        }
    })
};


//格式化数据 
const formartData = (data) => {
    Object.values(data).map((itemList: any) => {
        format(itemList);
        function format(itemList) {
            itemList.map(itemOptions => {
                let { key, value } = itemOptions;

                itemOptions.value = key;
                itemOptions['label'] = value;
                if (itemOptions.children) {
                    format(itemOptions.children)
                }
            })
        }

    })
    return data

}

//数据值等于 0 结构忽略
export const formartSearchValue = (serachFormValue) => {
    serachFormValue = JSON.parse(JSON.stringify(serachFormValue));
    Object.keys(serachFormValue).map(key => {
        if (serachFormValue[key] == "0") {
            serachFormValue[key] = "0";
        }
    });


    return serachFormValue
}
//收集权限
export const get_auth = () => {
    const setParams = (params) => {
        let authar = [];
        if (params?.auth_name ?? false) {
            authar.push('?get_auth=1')
            authar.push(`&auth_name=${params.auth_name}`)

        }


        return authar.join('')
    }

    const getAuthList = (res, params) => {
        let auth_list = res?.auth_list ?? {}
        authList[params.auth_name] = { ...auth_list }
    }

    return {
        setParams,
        getAuthList
    }
}

export let lang = (instance) => {
    let { proxy } = instance();
    lang = (key) => {
        return proxy.$t(key);
    }

}

export const resetUrl =  ( ) => { 
    let href = window.location.href;
    href=href.split("?")[0];
    window.history.pushState({},"",href);

     
}